<?php
/**
 * Created by PhpStorm.
 * User: HJJ
 * Date: 2022-11-10
 * Time: 10:43:01
 * Info: 登录控制器
 */

namespace app\api\controller;

use hg\apidoc\annotation as Apidoc;
use app\common\model\User as UserModel;
use app\common\model\UserPointLog as UserPointLogModel;

/**
 * @Apidoc\Title("会员接口")
 * @Apidoc\Group("base")
 * @Apidoc\Sort(0)
 */
class LoginController extends BaseController
{

    /**
     * @Apidoc\Title("会员登录")
     * @Apidoc\Desc("最基础的接口注释写法")
     * @Apidoc\Method("POST")
     * @Apidoc\Url("/api/login/index")
     * @Apidoc\Author("灰灰")
     * @Apidoc\Tag("登录")
     * @Apidoc\Param("mobile", type="string",require=true, desc="用户名")
     * @Apidoc\Param("password", type="string",require=true, desc="密码")
     * @Apidoc\Returned("id", type="int", desc="用户id")
     */
    public function index()
    {
        // $platform  = $this->request->param('platform', 2); //1就是h5登陆（h5端和微信公众号端），2就是微信小程序登陆，3是支付宝小程序，4是app，5是pc
        // $userModel = new UserModel();
        // $data  = $this->request->param();

        // $res = $userModel->toLogin($data, 2, $platform);
        // $ip_address = get_client_ip(0, true);// 获取当前请求的IP地址
        // //这里可以写入登录记录【未开发】

        // if ( ! empty($res['code']) && $res['code'] == 200) {
        //     $user  = $res['data']['user_info'];
        //     $token = $res['data']['token'];
        //     // 更新token信息和用户信息
        //     $token = $this->createTokenAndUpdateTime($token, $user, $ip_address);

        //     if ( ! $token) {
        //         $this->error("登录失败,请重试");
        //     }

        //     $this->success($res['msg'], $res['data']);
        // } else {
        //     $this->error($res['msg']);
        // }
        $code = $this->request->param('code');
         //配置appid
         $appid = "wxcdc378c2ee3920ec";
         //配置appscret
         $secret = "a82f1b3d83038268ecfa5bb1e6cceac7";
         //api接口
         $api = "https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$secret."&js_code=".$code."&grant_type=authorization_code";
 
         $rst = json_decode($this->httpSend($api,[],'GET'),TRUE);
         $this->success(200, $rst);
        //  $openId=$rst['openid'];
        //  $userInfo =  $this->logicUser->getUserInfo(['open_id' =>$openId]);
 
        //  if (empty($userInfo)) {
        //      $userupdate = ["open_id"=>$openId,"sex"=>$sex,'create_time'=>time(),'update_time'=>time(),'last_login'=>time(),'nick_name'=>$nickName,'head_pic'=>$headPic,'address'=>$address];
        //  } else {
        //      $userupdate = ["uid"=>$userInfo['uid'],"sex"=>$sex,'update_time'=>time(),'last_login'=>time(),'nick_name'=>$nickName,'head_pic'=>$headPic,'address'=>$address];
        //  }
        //  $res=$this->logicUser->editUser($userupdate);
        //  if(!empty($res)) {
        //      return [ 'code' => 200,'data'=>$this->logicUser->getUserInfo(['open_id' =>$openId]) ,'msg' => '成功'];
        //  }
 
    }

    /**
     * 登录获取信息
     *
     * @open_id
     * @token
     *
     * @return open_id
     */
    public function douserLogin()
    {
        // $nickName=$this->request->post('nick_name');
        // $headPic=$this->request->post('head_pic');
        // $openId=$this->request->post('open_id');
        // $sex=$this->request->post('sex');
        // $mobile=$this->request->post('mobile');
        // $userInfo =  $this->logicUser->getUserInfo(['open_id' =>$openId]);
        // if (empty($userInfo)) {
        //     $userupdate = ["open_id"=>$openId,"sex"=>$sex,"mobile"=>$mobile,'reg_time'=>date("H-m-d H:i:s"),'update_time'=>date("H-m-d H:i:s"),'last_login'=>date("H-m-d H:i:s"),'nick_name'=>$nickName,'head_pic'=>$headPic];
        // } else {
        //     $userupdate = ["uid"=>$userInfo['uid'],"sex"=>$sex,"mobile"=>$mobile,'update_time'=>date("H-m-d H:i:s"),'last_login'=>date("H-m-d H:i:s"),'nick_name'=>$nickName,'head_pic'=>$headPic];
        // }
        // $res=$this->logicUser->editUser($userupdate);
        // if(!empty($res)) {
        //     return json_encode(array('data'=>$this->logicUser->getUserInfo(['open_id' =>$openId]),'code'=>200,'msg'=>"成功"));
        // }
    }

    
    // 发送http请求
    protected function httpSend($url, $params, $method = 'GET', $header = array(), $multi = false){
        $opts = array(
            CURLOPT_TIMEOUT        => 30,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_HTTPHEADER     => $header
        );
        /* 根据请求类型设置特定参数 */
        switch(strtoupper($method)){
            case 'GET':
                $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
                break;
            case 'POST':
                //判断是否传输文件
                $params = $multi ? $params : http_build_query($params);
                $opts[CURLOPT_URL] = $url;
                $opts[CURLOPT_POST] = 1;
                $opts[CURLOPT_POSTFIELDS] = $params;
                break;
        }
        $ch = curl_init();
        curl_setopt_array($ch, $opts);
        $data  = curl_exec($ch);
        $error = curl_error($ch);
        curl_close($ch);
        if($error){
            return '请求出错'.$error;
        }
        return  $data;
    }

}